<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>AI 医疗助手</title>
    <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css" rel="stylesheet">
    <style>
        .chat-container {
            background-color: white;
            border-radius: 8px;
            box-shadow: 0 2px 4px rgba(0,0,0,0.1);
            height: calc(100% - 40px);
            padding: 20px;
            display: flex;
            flex-direction: column;
        }
        .chat-header {
            display: flex;
            align-items: center;
            margin-bottom: 20px;
        }
        .chat-header i {
            font-size: 24px;
            margin-right: 10px;
            color: #2196f3;
        }
        .chat-header h2 {
            margin: 0;
            color: #333;
        }
        .chat-box {
            flex-grow: 1;
            border: 1px solid #e0e0e0;
            border-radius: 8px;
            padding: 15px;
            margin-bottom: 20px;
            overflow-y: auto;
            background-color: #fff;
        }
        .message {
            margin: 10px 0;
            padding: 12px 15px;
            border-radius: 8px;
            max-width: 80%;
            word-wrap: break-word;
        }
        .user-message {
            background-color: #e3f2fd;
            margin-left: auto;
            color: #1976d2;
        }
        .bot-message {
            background-color: #f5f5f5;
            margin-right: auto;
            color: #333;
            white-space: pre-wrap;
        }
        .input-container {
            display: flex;
            gap: 10px;
            padding: 10px 0;
        }
        textarea {
            flex-grow: 1;
            padding: 12px;
            border: 1px solid #e0e0e0;
            border-radius: 8px;
            resize: none;
            min-height: 50px;
            font-size: 14px;
            transition: border-color 0.3s;
        }
        textarea:focus {
            outline: none;
            border-color: #2196f3;
        }
        button {
            padding: 12px 24px;
            background-color: #2196f3;
            color: white;
            border: none;
            border-radius: 8px;
            cursor: pointer;
            font-weight: 500;
            display: flex;
            align-items: center;
            gap: 8px;
            transition: background-color 0.3s;
        }
        button:disabled {
            background-color: #bdbdbd;
            cursor: not-allowed;
        }
        button:hover:not(:disabled) {
            background-color: #1976d2;
        }
        .status {
            padding: 8px 16px;
            border-radius: 8px;
            text-align: center;
            margin-bottom: 20px;
            font-weight: 500;
        }
        .status.online {
            background-color: #e8f5e9;
            color: #2e7d32;
        }
        .status.offline {
            background-color: #ffebee;
            color: #c62828;
        }
        .status i {
            margin-right: 8px;
        }
    </style>
</head>
<body>
    <div class="chat-container">
        <div class="chat-header">
            <i class="fas fa-robot"></i>
            <h2>AI 医疗助手</h2>
        </div>
        <div id="status" class="status">
            <i class="fas fa-circle-notch fa-spin"></i>
            正在检查连接状态...
        </div>
        <div id="chatBox" class="chat-box"></div>
        <div class="input-container">
            <textarea 
                id="messageInput" 
                placeholder="请输入您的问题，按回车发送（Shift + 回车换行）..."
                rows="2"
            ></textarea>
            <button id="sendButton" onclick="sendMessage()">
                <i class="fas fa-paper-plane"></i>
                发送
            </button>
        </div>
    </div>

    <script>
        const messageInput = document.getElementById('messageInput');
        const sendButton = document.getElementById('sendButton');
        let currentBotMessage = null;

        // 检查服务状态
        function checkStatus() {
            fetch('/api/deepseek/health')
                .then(response => response.text())
                .then(text => {
                    const statusDiv = document.getElementById('status');
                    if (text.includes('available')) {
                        statusDiv.className = 'status online';
                        statusDiv.innerHTML = '<i class="fas fa-circle"></i>AI 助手已连接';
                    } else {
                        statusDiv.className = 'status offline';
                        statusDiv.innerHTML = '<i class="fas fa-exclamation-circle"></i>AI 助手未连接';
                    }
                })
                .catch(error => {
                    const statusDiv = document.getElementById('status');
                    statusDiv.className = 'status offline';
                    statusDiv.innerHTML = '<i class="fas fa-exclamation-circle"></i>AI 助手未连接';
                });
        }

        // 发送消息
        function sendMessage() {
            const message = messageInput.value.trim();
            
            if (message) {
                // 禁用输入和发送按钮
                messageInput.disabled = true;
                sendButton.disabled = true;

                // 添加用户消息到聊天框
                addMessage(message, true);
                messageInput.value = '';

                // 创建新的bot消息div
                currentBotMessage = document.createElement('div');
                currentBotMessage.className = 'message bot-message';
                currentBotMessage.textContent = '';
                document.getElementById('chatBox').appendChild(currentBotMessage);

                // 创建 EventSource
                const eventSource = new EventSource('/api/deepseek/chat/stream?message=' + encodeURIComponent(message));

                eventSource.onmessage = function(event) {
                    if (currentBotMessage) {
                        // 添加换行符如果需要
                        if (currentBotMessage.textContent && !currentBotMessage.textContent.endsWith('\n')) {
                            currentBotMessage.textContent += event.data;
                        } else {
                            currentBotMessage.textContent += event.data;
                        }
                        currentBotMessage.scrollIntoView({ behavior: 'smooth', block: 'end' });
                    }
                };

                eventSource.onerror = function(error) {
                    console.error('EventSource error:', error);
                    eventSource.close();
                    if (currentBotMessage) {
                        if (currentBotMessage.textContent === '') {
                            currentBotMessage.textContent = '错误：无法获取AI响应，请稍后重试';
                        }
                        currentBotMessage = null;
                    }
                    // 重新启用输入和发送按钮
                    messageInput.disabled = false;
                    sendButton.disabled = false;
                };

                eventSource.addEventListener('complete', function(event) {
                    eventSource.close();
                    currentBotMessage = null;
                    // 重新启用输入和发送按钮
                    messageInput.disabled = false;
                    sendButton.disabled = false;
                });
            }
        }

        // 添加消息到聊天框
        function addMessage(message, isUser) {
            const chatBox = document.getElementById('chatBox');
            const messageDiv = document.createElement('div');
            messageDiv.className = `message ${isUser ? 'user-message' : 'bot-message'}`;
            messageDiv.textContent = message;
            chatBox.appendChild(messageDiv);
            messageDiv.scrollIntoView({ behavior: 'smooth', block: 'end' });
        }

        // 监听回车键发送消息
        messageInput.addEventListener('keypress', function(e) {
            if (e.key === 'Enter' && !e.shiftKey && !messageInput.disabled) {
                e.preventDefault();
                sendMessage();
            }
        });

        // 自动调整文本框高度
        messageInput.addEventListener('input', function() {
            this.style.height = 'auto';
            this.style.height = (this.scrollHeight) + 'px';
        });

        // 页面加载时检查状态
        checkStatus();
        // 每30秒检查一次状态
        setInterval(checkStatus, 30000);
    </script>
</body>
</html> 